import { UPDATEPAGEJOBVIEW } from '../types/pagejobview'
import { createAction } from 'redux-actions'
import { getListRequest } from '../../../../service/post-controller'
import {
  getIndustryCategoryRequest,
  queryArchiveForPageRequest
} from '../../../../service/user-archive-controller'
import { formatDataJobView, formatDataJobViewRight } from '../../../../service/format/job-view'
import { getCarouselMap } from '../../../../service/carousel-map'

export const DOREQUESTPEOPLE = createAction(
  UPDATEPAGEJOBVIEW,
  ({ dataSource = [], barlevecurrent, dataSourceAll, isOrder, ...queryPayload }) => {
    return new Promise(resolve => {
      queryArchiveForPageRequest({ ...queryPayload }).then(({ data }) => {
        const { status, entry, message } = data
        let haveNextPersonPage = true
        // 是否切换为老数据
        let changedOld = false
        if (!entry || entry.length < queryPayload.pageSize) {
          // 如果没有新数据了   则请求老数据
          if (!queryPayload.isOld) {
            // 去请求老数据
            queryPayload.isOld = true
            changedOld = true
          } else {
            // 老数据也没了  结束
            haveNextPersonPage = false
          }
          // if (isOrder) {
          //   // 筛选
          //   haveNextPersonPage = false
          // }
        }
        if ((!entry || entry.length < queryPayload.pageSize) && barlevecurrent !== 2) {
          // 数据小于pagesize 且不为全职，只有全职才有老数据
          haveNextPersonPage = false
        }

        // 老平台数据没有标签
        let isEmpty = false
        if (
          queryPayload.page == 1 &&
          (!entry || entry.length == 0) &&
          queryPayload.isOld &&
          dataSourceAll[0].dataSource.length == 0
        ) {
          isEmpty = true
        }

        // if (status) {
        if (queryPayload.page > 1 || queryPayload.isOld) {
          let entryOnline = formatDataJobViewRight(entry || [])
          const newDataSource = dataSource.concat(entryOnline)
          if (changedOld) {
            queryPayload.page = 1
          }
          let newdata = {
            haveNextPersonPage,
            showRightEmpty: isEmpty,
            dataSource: newDataSource,
            submitdata: {
              ...queryPayload
            }
          }
          const newdataquery = setDataSourceAllPerson(dataSourceAll, newdata, barlevecurrent)
          resolve({
            peopleDataSource: newdataquery,
            barlevecurrent
          })
        } else {
          const format = formatDataJobViewRight(entry || [])
          let newdata = {
            haveNextPersonPage,
            showRightEmpty: !(format && format.length),
            dataSource: format,
            submitdata: {
              ...queryPayload
            }
          }
          const newdataquery = setDataSourceAllPerson(dataSourceAll, newdata, barlevecurrent)
          resolve({
            peopleDataSource: newdataquery,
            barlevecurrent
          })
        }
        // } else {
        //   resolve({})
        // }
      })
    })
  }
)

const setDataSourceAllPerson = (dataSourceAll, newdata, barlevecurrent) => {
  let dataSourceReturn = []
  dataSourceAll.forEach((data, index) => {
    if (index == barlevecurrent) {
      dataSourceReturn.push(newdata)
    } else {
      dataSourceReturn.push(data)
    }
  })
  return dataSourceReturn
}

const setDataSourceAll = (dataSourceAll, newdata, barlevecurrent) => {
  let dataSourceReturn = []
  dataSourceAll.forEach((data, index) => {
    if (index == barlevecurrent) {
      dataSourceReturn.push(newdata)
    } else {
      dataSourceReturn.push(data)
    }
  })
  return dataSourceReturn
}

export const DOREQUESTJOBVIEW = createAction(
  UPDATEPAGEJOBVIEW,
  ({ dataSource = [], barlevecurrent, dataSourceAll, ...queryPayload }) => {
    return new Promise(resolve => {
      getListRequest({ ...queryPayload }).then(({ data }) => {
        const { status, entry, message } = data
        let haveNext = true
        if (!entry || entry.length < queryPayload.pageSize) {
          haveNext = false
        }

        // 老平台数据没有标签
        let isEmpty = false
        if (
          queryPayload.page == 1 &&
          (!entry || entry.length == 0) &&
          dataSourceAll[0].dataSource.length == 0
        ) {
          isEmpty = true
        }

        if (queryPayload.page > 1) {
          let entryOnline = formatDataJobView(entry || []) || []
          const newDataSource = dataSource.concat(entryOnline)
          let newdata = {
            haveNextJobPage: haveNext,
            showLeftEmpty: isEmpty,
            dataSource: newDataSource,
            submitdata: {
              ...queryPayload
            }
          }
          let dataSourceFormat = setDataSourceAll(dataSourceAll, newdata, barlevecurrent)
          resolve({ dataSource: dataSourceFormat, barlevecurrent })
        } else {
          const format = formatDataJobView(entry || [])
          let newdata = {
            haveNextJobPage: haveNext,
            showLeftEmpty: !(format && format.length),
            dataSource: format,
            submitdata: {
              ...queryPayload
            }
          }
          let dataSourceFormat = setDataSourceAll(dataSourceAll, newdata, barlevecurrent)
          resolve({ dataSource: dataSourceFormat, barlevecurrent })
        }
      })
    })
  }
)

export const getJobCateList = createAction(UPDATEPAGEJOBVIEW, type => {
  return new Promise(resolve => {
    getIndustryCategoryRequest(type).then(({ data }) => {
      const { entry, code } = data
      if (code === 200) {
        resolve({
          desireList: [...entry]
        })
      } else {
        resolve({
          desireList: []
        })
      }
    })
  })
})

export const DOREQUESTBANNER = createAction(UPDATEPAGEJOBVIEW, payload => {
  // 获取首页banner
  return new Promise(resolve => {
    getCarouselMap().then(({ data }) => {
      resolve({
        homePageBanner: data.data
      })
    })
  })
})
